home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / cppcom.zip / COMPORTS.CPP < prev    next >
C/C++ Source or Header  |  1991-05-25  |  10KB  |  211 lines

  1. /***************************************************************************
  2. These C++ classes are copyright 1990, by William Herrera.
  3. All those who put this code or its derivatives in a commercial product MUST
  4. mention this copyright in their documentation for users of the products in
  5. which this code or its derivative classes are used.  Otherwise, this code
  6. may be freely distributed and freely used for any purpose.
  7.  
  8. Enhancements: 1991 by David Orme
  9.     *  General cleanup.
  10.             - I/O now takes advantage of C++ overloading.
  11.             - Serial port I/O functionality now only in Serial class.
  12.             - Modem functionality now only in Modem class.
  13.     *  Possible to easily implement file Xfr prots now.
  14.     *  CCITT CRC-16 class added                            -- 2-20-1991
  15.     *  BIOS Timer class added                                -- 2-22-1991
  16.     *  Optional timeout on all input routines added    -- 2-25-1991
  17.  
  18. ***************************************************************************/
  19.  
  20. // file comports.cpp defines various uart classes.
  21.  
  22. #include <stdlib.h>
  23.  
  24. #include "comports.hpp"
  25.  
  26. static const int insize = 1024;
  27. static const int outsize = 4096;
  28.  
  29. static void DoNothing(void) { return; }
  30.  
  31. // C++ 2.0 requires that static members be declared globally...
  32.  
  33. COM1 * COM1::this_ptr;
  34. COM2 * COM2::this_ptr;
  35. COM3 * COM3::this_ptr;
  36. COM4 * COM4::this_ptr;
  37.  
  38.  
  39. #ifdef __TURBOC__
  40.  
  41. #define DEFINE_COMX(COM, x, driver)                     \
  42. CharQueue * COM##x::inq = new CharQueue(insize);        \
  43. CharQueue * COM##x::outq = new CharQueue(outsize);      \
  44. DRIVER COM##x::driver = driver##x;                      \
  45. COM##x::COM##x()                                        \
  46. {                                                       \
  47.     this_ptr = this;                                    \
  48.     DoIfNoCarrier = DoNothing;                          \
  49.     DoOnRing = DoNothing;                                \
  50.     if (RegisterDriver(##x, driver##x) != 0)             \
  51.     exit(1);                                        \
  52. }                                                       \
  53. COM##x::~COM##x()                                       \
  54. {                                                       \
  55.     if (RestoreDriver(##x) != 0)                         \
  56.     exit(1);                                        \
  57. }                                                       \
  58. void COM##x::SetDoIfNoCarrier(void (*f)())              \
  59. {                                                       \
  60.     DoIfNoCarrier = f;                                  \
  61. }                                                       \
  62. void COM##x::SetDoOnRing(void (*f)())                    \
  63. {                                                       \
  64.     DoOnRing = f;                                        \
  65. }                                                       \
  66. void COM##x::FlushOutput()                              \
  67.     { while (!outq->IsEmpty()) {;} }                    \
  68. void COM##x::SendChar(char ch)                          \
  69. {                                                       \
  70.     while (outq->IsFull())                              \
  71.     ;    /* wait til queue transmitted by driver */ \
  72.     outq->Add(ch);                                      \
  73.     SetIER_Transmit(true);                              \
  74. }                                                       \
  75. int COM##x::GetChar()                                   \
  76. {                                                       \
  77.     return inq->Get();                                  \
  78.     /* remember, empty charqueue flag is -1. */         \
  79. }                                                      \
  80.                             \
  81. void interrupt driver##x(...)                           \
  82. {                                                       \
  83.     /* driver for comm interrupt. */                    \
  84.     COM##x * cptr = COM##x::this_ptr;                   \
  85.     com_interrupt_t i;                                  \
  86.     while ((i = cptr->GetIntrType()) != NONE_PENDING)   \
  87.     {                                                   \
  88.     disable();  /* don't let another come in */     \
  89.     switch ((int)i & 0xFF)                          \
  90.     {                                               \
  91.         case (int)TRANSMIT_READY :                  \
  92.         if (COM##x::outq->IsEmpty())            \
  93.             cptr->SetIER_Transmit(false);       \
  94.         else                                    \
  95.             cptr->                              \
  96.             TransmitChar(COM##x::outq->Get());  \
  97.         break;                                  \
  98.         case (int)RECEIVE_READY:                    \
  99.         if (!COM##x::inq->IsFull())             \
  100.             COM##x::inq->                       \
  101.             Add(cptr->ReceiveChar());       \
  102.         break;                                  \
  103.         case (int)NO_CARRIER:                       \
  104.         (*cptr->DoIfNoCarrier)();               \
  105.         break;                                  \
  106.         case (int)RING:                                \
  107.         (*cptr->DoOnRing)();                    \
  108.         break;                                     \
  109.         default:                                    \
  110.         /* all others here. the function */         \
  111.         /* GetIntrType() should have already */     \
  112.         /* cleaned up the other interrupt flags. */ \
  113.         break;                                  \
  114.     }                                               \
  115.     enable();  /* other interrupts now ok */        \
  116.     }                                                   \
  117.     outportb( 0x20, 0x20);                              \
  118. }
  119.  
  120.  
  121.  
  122.  
  123. #else ifdef __ZTC__
  124.  
  125. #define DEFINE_COMX(COM, x, driver)                     \
  126. CharQueue * COM##x::inq = new CharQueue(insize);        \
  127. CharQueue * COM##x::outq = new CharQueue(outsize);      \
  128. DRIVER COM##x::driver = driver##x;                      \
  129. COM##x::COM##x()                                        \
  130. {                                                       \
  131.     this_ptr = this;                                    \
  132.     DoIfNoCarrier = DoNothing;                          \
  133.     DoOnRing = DoNothing;                                \
  134.     if(RegisterDriver(##x, driver##x) != 0)             \
  135.     exit(1);                                        \
  136. }                                                       \
  137. COM##x::~COM##x()                                       \
  138. {                                                       \
  139.     if(RestoreDriver(##x) != 0)                         \
  140.     exit(1);                                        \
  141. }                                                       \
  142. void COM##x::SetDoIfNoCarrier(void (*f)())              \
  143. {                                                       \
  144.     DoIfNoCarrier = f;                                  \
  145. }                                                       \
  146. void COM##x::SetDoOnRing(void (*f)())                    \
  147. {                                                       \
  148.     DoOnRing = f;                                        \
  149. }                                                       \
  150. void COM##x::FlushOutput()                              \
  151.     { while (!outq->IsEmpty()) {;} }                    \
  152. void COM##x::SendChar(char ch)                          \
  153. {                                                       \
  154.     while(outq->IsFull())                               \
  155.     ;                                               \
  156.     outq->Add(ch);                                      \
  157.     SetIER_Transmit(true);                              \
  158. }                                                       \
  159. int COM##x::GetChar()                                   \
  160. {                                                       \
  161.     return inq->Get();                                  \
  162. }                                                       \
  163.                             \
  164. int driver##x(INT_DATA *pd)                             \
  165. {                                                       \
  166.     COM##x * cptr = COM##x::this_ptr;                   \
  167.     com_interrupt_t i;                                  \
  168.     while((i = cptr->GetIntrType()) != NONE_PENDING)    \
  169.     {                                                   \
  170.     disable();  /* don't let another come in */     \
  171.     switch((int)i & 0xFF)                           \
  172.     {                                               \
  173.         case (int)TRANSMIT_READY :                  \
  174.         if(COM##x::outq->IsEmpty())             \
  175.             cptr->SetIER_Transmit(false);       \
  176.         else                                    \
  177.             cptr->                              \
  178.             TransmitChar(COM##x::outq->Get());  \
  179.               break;                                  \
  180.             case (int)RECEIVE_READY:                    \
  181.                 if(!COM##x::inq->IsFull())              \
  182.                     COM##x::inq->                       \
  183.                         Add(cptr->ReceiveChar());       \
  184.                 break;                                  \
  185.         case (int)NO_CARRIER:                       \
  186.                 (*cptr->DoIfNoCarrier)();               \
  187.              break;                                  \
  188.             case (int)RING:                                \
  189.                 (*cptr->DoOnRing)();                    \
  190.              break;                                     \
  191.             default:                                    \
  192.         break;                                  \
  193.         }                                               \
  194.         enable();  /* other interrupts now ok */        \
  195.     }                                                   \
  196.     outportb( 0x20, 0x20);                              \
  197.     return 1;                                           \
  198. }                                                       
  199.  
  200.  
  201. #endif
  202.  
  203. // To define up to four com ports, you should
  204. // declare one or more of the following statements:
  205.  
  206. DEFINE_COMX(COM, 1, driver)
  207. DEFINE_COMX(COM, 2, driver)
  208. DEFINE_COMX(COM, 3, driver)
  209. DEFINE_COMX(COM, 4, driver)
  210.  
  211.